//
//  ByViewNLoginSegment.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewNLoginSegment.h"

@interface ByViewNLoginSegment()
{
    NSInteger _prevTag; // 上个选中按钮的tag
}

@property (nonatomic, strong) UIScrollView *scroll; // 底部滑动视图
@property (nonatomic, strong) UIView *line;         // 底部颜色线

@end


@implementation ByViewNLoginSegment

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
    }
    
    
    return self;
}


- (UIScrollView *)scroll {
    if (!_scroll) {
        UIView *view = [[UIView alloc] initWithFrame:self.bounds];
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        
        _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scroll];
    }
    return _scroll;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = BY_THEME_COLOR;
    }
    return _line;
}




- (void)setTitlesAry:(NSArray *)titlesAry {
    _titlesAry = titlesAry;
    [self createScrollView];
}

- (void)setIndex:(NSInteger)index {
    UIButton *button = (UIButton *)[self viewWithTag:index + 10];
    [self buttonAction:button];
}
- (void)buttonAction:(UIButton *)button {
    
    // 点击了已经选中的按钮
    if (button.tag == _prevTag) {
        return;
    }
    // 更改上个呗选中的按钮的状态
    UIButton *prevButton = (UIButton *)[self viewWithTag:_prevTag];
    prevButton.selected = NO;
    
    // 更改上个被选中的索引
    button.selected = YES;
    _prevTag = button.tag;
    
    [UIView animateWithDuration:0.5 animations:^{
        _line.center = [self viewWithTag:_prevTag].center;
        _line.bottom = self.height;
    } completion:nil];
    
    
    
    if (self.indexChangeBlock) {
        self.indexChangeBlock(button.tag - 10);
    }

}





#pragma mark UI
- (void)createScrollView {
    
    CGFloat w = self.width / _titlesAry.count;
    for (NSInteger i = 0; i < _titlesAry.count ; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(w*i, 0, w, self.height);
        button.titleLabel.textColor = BY_THEME_COLOR;
        button.tag = 10 + i;
        NSString *title = _titlesAry[i];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:BY_THEME_COLOR forState:UIControlStateSelected];
        button.titleLabel.font = FONT(14);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scroll addSubview:button];
        if (button.tag == 10) {
            button.selected = YES;
            _prevTag = button.tag;
        }
        
    }
    
    self.scroll.contentSize = CGSizeMake(self.width, 0);
    

    [self.scroll setContentOffset:CGPointMake(0, 0) animated:YES];
    // 创建底部线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
    line.backgroundColor = BY_LINE_COLOR;
    line.bottom = self.height;
    [self addSubview:line];

    self.line.frame = CGRectMake(0, 0, w, 2);
    self.line.center = [self viewWithTag:_prevTag].center;
    self.line.bottom = self.scroll.bottom;
    [self.scroll addSubview:self.line];
    
}


@end
